<template>
  <div class="centered-capsule-tabs">
    <div class="tabs-container">
      <div
        v-for="(tab, index) in tabs"
        :key="index"
        class="tab-item"
        :class="{ 'active': currentIndex === index }"
        @click="switchTab(index)"
        ref="tabItems"
      >
        <span class="tab-text">{{ tab.label }}</span>
      </div>
      <div
        class="slider"
        :style="sliderStyle"
      ></div>
    </div>
    <div class="tab-content">
      <slot :name="`tab-${currentIndex}`"></slot>
    </div>
  </div>
</template>

<script>
export default {
  name: 'CenteredCapsuleTabs',
  props: {
    tabs: {
      type: Array,
      required: true,
      validator: value => value.every(item => 'label' in item)
    },
    initialTab: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      currentIndex: this.initialTab,
      itemWidths: [],
      itemPositions: []
    }
  },
  computed: {
    sliderStyle() {
      if (this.itemWidths.length === 0) return {};

      return {
        width: `${this.itemWidths[this.currentIndex]}px`,
        transform: `translateX(${this.itemPositions[this.currentIndex]}px)`,
        transition: 'transform 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55), width 0.3s ease'
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.calculateItemDimensions();
      window.addEventListener('resize', this.handleResize);
    });
  },
  activated() {
    this.$nextTick(() => {
      this.calculateItemDimensions();
    });
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.handleResize);
  },
  methods: {
    switchTab(index) {
      if (this.currentIndex === index) return;
      this.currentIndex = index;
      this.$emit('tab-change', index);
    },
    calculateItemDimensions() {
      const items = this.$refs.tabItems;
      if (!items) return;

      this.itemWidths = items.map(item => item.offsetWidth);

      let accumulatedWidth = 0;
      this.itemPositions = items.map((item, index) => {
        const position = accumulatedWidth;
        accumulatedWidth += item.offsetWidth + 16; // 16px 是 margin
        return position;
      });
    },
    handleResize() {
      this.calculateItemDimensions();
    }
  }
}
</script>

<style scoped>
.centered-capsule-tabs {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.tabs-container {
  display: inline-flex;
  position: relative;
  background-color: #f0f2f5;
  border-radius: 50px;
  padding: 6px 0;
  margin-bottom: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  overflow-x: auto;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
  scroll-snap-type: x mandatory;
  &::-webkit-scrollbar {
    display: none;
  }
}

.tab-item {
  position: relative;
  min-width: 80px;
  padding: 8px 0;
  margin: 0 8px;
  border-radius: 50px;
  font-size: 14px;
  font-weight: 500;
  color: #666;
  cursor: pointer;
  transition: all 0.3s ease;
  z-index: 1;
  user-select: none;
  display: flex;
  justify-content: center;
  align-items: center;
}

.tab-text {
  padding: 0 16px;
  text-align: center;
  white-space: nowrap;
}

.tab-item.active {
  color: #fff; 
}

.slider {
  position: absolute;
  height: calc(100% - 12px);
  background-color: #1890ff;
  border-radius: 50px;
  top: 6px;
  left: 6px;
  z-index: 0;
  box-shadow: 0 2px 4px rgba(24, 144, 255, 0.3);
}

.tab-content {
  padding: 5px;
  width: 100%;
  animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
  from { opacity: 0.5; transform: translateY(5px); }
  to { opacity: 1; transform: translateY(0); }
}
</style>
